import {MainData} from "../../coreModule/data/BaseData";
import {DataOccasion, field, occasion} from "../../coreModule/data/DataLoader";
import {dataClass} from "../../coreModule/managers/DataManager";
import {Constructor} from "../../../utils/BaseContext";
import {BaseRepository, getRepository, repository} from "../../coreModule/data/BaseRepository";
import {DateUtils} from "../../../utils/DateUtils";

export enum NoticeStatus{
	Normal, Deleted
}

@dataClass("Notice")
export class Notice extends MainData {

	@field
	public title: string = "";
	@field
	public content: string = "";
	@field(Number)
	public createdAt: number;
	@field(Number)
	public startAt: number; // 开始时间（为空则即刻开始)
	@field(Number)
	public endAt: number; // 结束时间（为空则不会结束）
	@field
	public status: NoticeStatus = NoticeStatus.Normal;

	// region 额外数据

	@field(String)
	@occasion(DataOccasion.Extra)
	createTimeStr: string

	public refresh() {
		this.createTimeStr = DateUtils.time2Str(this.createdAt, false);
	}

	// endregion

	/**
	 * 创建
	 */
	public static create(title: string, content: string,
											 startAt?: number, endAt?: number) {
		const res = new Notice();
		res.title = title;
		res.content = content;
		res.startAt = startAt;
		res.endAt = endAt;

		res.createdAt = Date.now();

		return res;
	}
}

export function noticeRepo() {
	return getRepository(NoticeRepo);
}

@repository
export class NoticeRepo extends BaseRepository<Notice> {

	get clazz(): Constructor<Notice> { return Notice; }
	public constructor() { super(); }
}
